
#include "stdio.h"
#include "stdlib.h"

struct _hlib_lps_functions    hlib_lps_functions;
struct _hlib_lps_global_state hlib_lps_global_state;



enum hlib_lps_ret  create_channel_common
      (struct hlib_channel_handle * channel_handle,
       const char * channel_name,
       uint32_t endpoint_count,
       size_t buffer_in_size,
       size_t buffer_out_size)
{ 
  uint32_t channel_handle_temp =
    hlib_lps_global_state.active_channel_count+1;
  
  struct hlib_lps_channel * channel = 
    & (hlib_lps_global_state.channel_data[channel_handle_temp]);
  
  channel -> channel_name = channel_name;
  channel -> hard_endpoint_count = endpoint_count;
  
  //allocate the endpoint opaque type
  channel -> data = (struct hlib_lps_pair_end *)
    calloc(endpoint_count , sizeof(struct hlib_lps_pair_end));
  if(channel -> data == NULL)
    return hlib_lps_mem_error;

  //allocate the buffer common type
  channel -> buffer_in_arr = (void *)
    calloc(endpoint_count , buffer_in_size);
  if( channel -> buffer_in_arr == NULL)
    return hlib_lps_mem_error;

  channel -> buffer_out_arr = (void *)
    calloc(endpoint_count , buffer_out_size);
  if(channel -> buffer_out_arr == NULL)
    return hlib_lps_mem_error;

  channel_handle -> data = channel_handle_temp;

  return hlib_lps_ok;
}

enum hlib_lps_ret init_server_common
    (const char * base_name,
     uint32_t hard_channel_limit)
{
  hlib_lps_global_state.base_name=base_name;
  hlib_lps_global_state.hard_channel_limit=
    hard_channel_limit;
  
  hlib_lps_global_state.active_channel_count=0;
  
  hlib_lps_global_state.channel_data = (struct hlib_lps_channel *)
    calloc(hard_channel_limit  ,sizeof(struct hlib_lps_channel));
  
  if(hlib_lps_global_state.channel_data == NULL)
    return hlib_lps_mem_error;

  return hlib_lps_ok;
}

enum hlib_lps_ret hlib_lps_init()
{
  hlib_lps_global_state.func_profile_name
    = "Unix Local Pipe Server -  Profile: Normal";

  hlib_lps_functions.init_server=
    init_server_common;
  hlib_lps_functions.create_channel=
    create_channel_common;

  return hlib_lps_ok;
}


int main()
{
  hlib_lps_init();
  
  hlib_lps_functions.init_server("/tmp/",16);

  printf("%s\n", hlib_lps_global_state.func_profile_name);
  return 0;
}
